<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header @addtodos="addtodos" />
      <Main :checkedtodos="checkedtodos" :deleteTodo="deleteTodo" :todos="todos" />
      <Footer :todos="todos" @isSelectAll="isSelectAll" @deeleteTodoAll="deeleteTodoAll"/>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";

export default defineComponent({
  name: "App",
});
</script>

<script setup lang="ts">
import Header from "./components/header/index.vue";
import Main from "./components/main/index.vue";
import Footer from "./components/footer/index.vue";
import { nanoid } from "nanoid";
export interface todoItemType {
  id: string;
  title: string;
  comment: boolean;
}
export type todosType = todoItemType[];
const todos = ref<todosType>([
  { id: nanoid(), title: "吃饭", comment: false },
  { id: nanoid(), title: "睡觉", comment: true },
  { id: nanoid(), title: "唱歌", comment: false },
  { id: nanoid(), title: "跳舞", comment: false },
]);
//添加todo
const addtodos = (title: string) => {
  const newTodo = { id: nanoid(), title, comment: false };
  todos.value.unshift(newTodo);
};
//是否选中任务
const checkedtodos = (index: number) => {
  todos.value[index].comment = !todos.value[index].comment;
};
//删除任务
const deleteTodo = (index:number) =>{
  todos.value.splice(index,1)
}
//全选
const isSelectAll = (newType: boolean)=>{
  todos.value.forEach(item=>item.comment = newType)
}
//删除所有完成任务
const deeleteTodoAll = ()=>{
  todos.value = todos.value.filter(item=>{
    return !item.comment
  })
}
</script>

<style>
body {
  background: pink;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn:focus {
  box-shadow: none;
}

/*app*/
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
